<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="数据字典名或编号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getSystemdictionarytypes">查询</el-button>
				</el-form-item>
        <el-form-item>
          <el-button type="primary"  v-on:click="handleAdd">新增</el-button>
        </el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table @cell-dblclick="showDrawer" :data="systemdictionarytypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="40">
			</el-table-column>
			<el-table-column prop="name"  label="数据字典名" width="360" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="360" sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="visible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="120" :rules="editFormRules" ref="editForm">
				<el-form-item label="数据字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="visible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


    <el-dialog :title="detailTitle" :visible.sync="detailVisible" :close-on-click-modal="false">
      <el-form :model="editDetailForm" label-width="120" :rules="editDetailFormRules" ref="editDetailForm">
        <el-form-item label="数据字典名" prop="name">
          <el-input v-model="editDetailForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-select v-model="editDetailForm.type" value-key="id"  placeholder="请选择">
          <el-option
              v-for="t in types"
              :label="t.name"
              :value="t">
            <span style="float: left">{{ t.name }}</span>
            <span style="float: right; font-size: 13px">{{ t.sn }}</span>
          </el-option>
        </el-select><br>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detailVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editDetailSubmit" :loading="detailEditLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-drawer
        size="800px"
        title="数据具体"
        :visible.sync="drawer"
        :direction="direction"
        :before-close="handleClose">
      <span>

           <el-form :inline="true" :model="detailFilters">
              <el-form-item>
                <el-input v-model="detailFilters.name" placeholder="数据字典名"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" v-on:click="getSystemdictionarydetails">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="primary"  v-on:click="handleDetailAdd">新增</el-button>
              </el-form-item>
            </el-form>

        		<el-table  :data="systemdictionarydetails" highlight-current-row v-loading="qdldqlistLoading" @selection-change="selsDetailChange" style="width: 100%;">
              <el-table-column type="selection" width="55">
              </el-table-column>
              <el-table-column type="index" width="40">
              </el-table-column>
              <el-table-column prop="name"  label="数据字典名" width="200" sortable>
              </el-table-column>
              <el-table-column prop="type.name"  label="类型" width="200" sortable>
              </el-table-column>
              <el-table-column label="操作" width="200">
                <template scope="scope">
                  <el-button size="small" @click="handleDetailEdit(scope.$index, scope.row)">编辑</el-button>
                  <el-button type="danger" size="small" @click="handleDetailDel(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

        		<el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchDetailRemove" :disabled="this.detailSels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleDetailCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
          </el-col>
      </span>
    </el-drawer>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getSystemdictionarytypeListPage, removeSystemdictionarytype, batchRemoveSystemdictionarytype, editSystemdictionarytype, addSystemdictionarytype } from '../../api/api';

	export default {
		data() {
			return {
        detailId:0,
        qdldqlistLoading:false,
        drawer:false,
        title:'',
        detailTitle:'',
        direction: 'rtl',
				filters: {
					name: ''
				},

        detailFilters: {
          name: ''
        },

				systemdictionarytypes: [],
        systemdictionarydetails:[],
        types:[],
        departments:[],
        shops:[],
				total: 0,
				currentPage: 1,
        pageSize:6,
				listLoading: false,
				sels: [], //列表选中列
        detailSels:[],

				visible: false,//编辑界面是否显示
        detailVisible:false,

				editLoading: false,
        detailEditLoading:false,


				editFormRules: {
					name: [
						{ required: true, message: '请输入字典数据名', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入编号', trigger: 'blur' }
					]
				},

        editDetailFormRules: {
          name: [
            { required: true, message: '请输入字典数据名', trigger: 'blur' }
          ],
        },

				//编辑界面数据
				editForm: {
          name:'',
          sn:''
				},

        editDetailForm: {
          name:'',
          type:''
        },

				addLoading: false,
			}
		},
		methods: {
      handleClose(done) {
        this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});
      },
      showDrawer(row){
        this.drawer = true
        this.detailId =row.id
        this.getSystemdictionarydetails()
        this.getTypes()
      },
			//性别显示转换
			formatState: function (row, column) {
				return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getSystemdictionarytypes();
			},

      handleDetailCurrentChange(val) {
        this.currentPage = val;
        this.getSystemdictionarydetails();
      },

			//获取字典数据类型列表
			getSystemdictionarytypes() {
				let para = {
					currentPage: this.currentPage,
					pageSize:this.pageSize,
          name:this.filters.name
				};
				this.listLoading = true;
				//NProgress.start();
        this.$http.post("/systemdictionarytype",para)
            .then((res) => {
					this.total = res.data.total;
          let data = res.data.data
              for (let i in data) {
                for (let j in data[i]) {
                    if(!data[i][j]){
                      data[i][j] = ''
                    }
                }
              }
          this.systemdictionarytypes = data
					this.listLoading = false;
					//NProgress.done();
				});
			},

      getSystemdictionarydetails() {
        let para = {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
          name:this.detailFilters.name,
          id:this.detailId
        };
        this.qdldqlistLoading = true;
        //NProgress.start();
        this.$http.post("/systemdictionarydetail",para)
            .then((res) => {
              this.total = res.data.total;
              let data = res.data.data
              for (let i in data) {
                for (let j in data[i]) {
                  if(!data[i][j]){
                    data[i][j] = ''
                  }
                }
              }
              this.systemdictionarydetails = data
              this.qdldqlistLoading = false;
              //NProgress.done();
            });
      },


      getTypes() {
        this.qdldqlistLoading = true;
        //NProgress.start();
        this.$http.get("/systemdictionarytype/")
            .then((res) => {
              this.types = res.data
              // this.total = res.data.total;
              // let data = res.data.data
              // for (let i in data) {
              //   for (let j in data[i]) {
              //     if(!data[i][j]){
              //       data[i][j] = ''
              //     }
              //   }
              // }
              // this.systemdictionarydetails = data
              // this.qdldqlistLoading = false;
              //NProgress.done();
            });
      },


			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// let para = { id: row.id };
					// removeSystemdictionarytype(para)
          this.$http.delete("/systemdictionarytype/"+row.id)
              .then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getSystemdictionarytypes();
					});
				}).catch(() => {

				});
			},

      //删除
      handleDetailDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.qdldqlistLoading = true;
          //NProgress.start();
          // let para = { id: row.id };
          // removeSystemdictionarytype(para)
          this.$http.delete("/systemdictionarydetail/"+row.id)
              .then((res) => {
                this.qdldqlistLoading = false;
                //NProgress.done();
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.getSystemdictionarydetails();
              });
        }).catch(() => {

        });
      },

			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.visible = true;
				this.editForm = Object.assign({}, row);
			},

      handleDetailEdit: function (index, row) {
        this.detailTitle="编辑";
        this.detailVisible = true;
        this.editDetailForm = Object.assign({}, row);
      },

			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
				this.visible = true;
				this.editForm = {
            name:'',
            sn:''
				};
			},

      handleDetailAdd: function () {
        this.detailTitle = "新增";
        this.detailVisible = true;
        this.editDetailForm = {
          name:'',
          type: ''
        };
      },

			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							this.$http.put("/systemdictionarytype",para)
                  .then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.visible = false;
								this.getSystemdictionarytypes();
							});
						});
					}
				});
			},

      editDetailSubmit: function () {
        this.$refs.editDetailForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.detailEditLoading = true;
              let para = Object.assign({}, this.editDetailForm);
              para.type = {"id":this.editDetailForm.type.id}
              this.$http.put("/systemdictionarydetail",para)
                  .then((res) => {
                    this.detailEditLoading = false;
                    //NProgress.done();
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                    this.$refs['editDetailForm'].resetFields();
                    this.detailVisible = false;
                    this.getSystemdictionarydetails();
                  });
            });
          }
        });
      },

			selsChange: function (sels) {
				this.sels = sels;
			},

      selsDetailChange: function (detailSels) {
        this.detailSels = detailSels;
      },


			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          this.$http.patch("/systemdictionarytype/delete",ids)
              .then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getSystemdictionarytypes();
					});
				}).catch(() => {

				});
			},

      batchDetailRemove: function () {
        var ids = this.detailSels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.qdldqlistLoading = true;
          //NProgress.start();
          this.$http.patch("/systemdictionarydetail/delete",ids)
              .then((res) => {
                this.qdldqlistLoading = false;
                //NProgress.done();
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.getSystemdictionarydetails();
              });
        }).catch(() => {

        });
      }


		},
		mounted() {
			this.getSystemdictionarytypes();
		}
	}

</script>

<style scoped>

</style>